package view;

import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JTable;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;

import controller.EmailController;

import java.awt.Dimension;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;

public class EmailPanel extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	JLabel addressLabel = new JLabel("Email Address:");
	JLabel pwdLabel = new JLabel("Password:");
	JLabel serverLabel = new JLabel("Server Address:");
	JLabel emailLabel = new JLabel("Email:");
	JTextField addressField = new JTextField();
	JPasswordField pwdField = new JPasswordField();
	JTextField serverField = new JTextField();
	JButton modifyButton = new JButton("Modify");
	JButton addButton = new JButton("Add");
	JButton removeButton = new JButton("Remove");
	EmailTableModel emailTableModel = new EmailTableModel();
	JTable emailTable = new JTable(emailTableModel);

	EmailController emailController;
	TaskFrame taskFrame;

	public EmailPanel(TaskFrame frame) {

		this.taskFrame = frame;
		emailController = new EmailController(this);

		GridBagLayout gbl = new GridBagLayout();
		GridBagConstraints gbc = new GridBagConstraints();

		this.setLayout(gbl);
		// Label
		gbc.gridx = 0;
		gbc.gridy = 0;
		gbc.anchor = GridBagConstraints.WEST;
		gbl.setConstraints(emailLabel, gbc);
		this.add(emailLabel);

		gbc.gridx = 1;
		gbc.gridy = 0;
		gbc.anchor = GridBagConstraints.EAST;
		gbl.setConstraints(addressLabel, gbc);
		this.add(addressLabel);

		gbc.gridy = 1;
		gbl.setConstraints(pwdLabel, gbc);
		this.add(pwdLabel);

		gbc.gridy = 2;
		gbl.setConstraints(serverLabel, gbc);
		this.add(serverLabel);
		gbc.anchor = GridBagConstraints.CENTER;

		// TextField
		gbc.gridx = 2;
		gbc.gridy = 0;
		gbc.gridwidth = 2;
		gbc.fill = GridBagConstraints.HORIZONTAL;
		gbl.setConstraints(addressField, gbc);
		this.add(addressField);

		gbc.gridy = 1;
		gbl.setConstraints(pwdField, gbc);
		this.add(pwdField);

		gbc.gridy = 2;
		gbl.setConstraints(serverField, gbc);
		this.add(serverField);
		gbc.fill = GridBagConstraints.NONE;

		// Buttons
		gbc.gridx = 1;
		gbc.gridy = 3;
		gbc.anchor = GridBagConstraints.SOUTHEAST;
		gbc.gridwidth = 3;
		JPanel panel = new JPanel();
		panel.add(removeButton);
		panel.add(modifyButton);
		panel.add(addButton);
		gbl.setConstraints(panel, gbc);
		this.add(panel);

		// Table
		gbc.gridx = 0;
		gbc.gridy = 1;
		gbc.gridwidth = 1;
		gbc.gridheight = 3;
		emailTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		emailTable.setPreferredScrollableViewportSize(new Dimension(300, 150));
		this.add(new JScrollPane(emailTable), gbc);

		// addListener
		addButton.addActionListener(emailController);
		removeButton.addActionListener(emailController);
		modifyButton.addActionListener(emailController);
		emailTable.getSelectionModel()
				.addListSelectionListener(emailController);

	}

	public JButton getAddButton() {
		return addButton;
	}

	public JTextField getAddressField() {
		return addressField;
	}

	public JLabel getAddressLabel() {
		return addressLabel;
	}

	public EmailController getEmailController() {
		return emailController;
	}

	public JLabel getEmailLabel() {
		return emailLabel;
	}

	public JTable getEmailTable() {
		return emailTable;
	}

	public EmailTableModel getEmailTableModel() {
		return emailTableModel;
	}

	public JButton getModifyButton() {
		return modifyButton;
	}

	public JPasswordField getPwdField() {
		return pwdField;
	}

	public JLabel getPwdLabel() {
		return pwdLabel;
	}

	public JButton getRemoveButton() {
		return removeButton;
	}

	public JTextField getServerField() {
		return serverField;
	}

	public JLabel getServerLabel() {
		return serverLabel;
	}

	public TaskFrame getTaskFrame() {
		return taskFrame;
	}

	public void init() {
		this.removeButton.setEnabled(false);
		this.modifyButton.setEnabled(false);
	}

	public void setAddButton(JButton addButton) {
		this.addButton = addButton;
	}

	public void setAddressField(JTextField addressField) {
		this.addressField = addressField;
	}

	public void setAddressLabel(JLabel addressLabel) {
		this.addressLabel = addressLabel;
	}

	public void setEmailController(EmailController emailController) {
		this.emailController = emailController;
	}

	public void setEmailLabel(JLabel emailLabel) {
		this.emailLabel = emailLabel;
	}

	public void setEmailTable(JTable emailTable) {
		this.emailTable = emailTable;
	}

	public void setEmailTableModel(EmailTableModel emailTableModel) {
		this.emailTableModel = emailTableModel;
	}

	public void setModifyButton(JButton modifyButton) {
		this.modifyButton = modifyButton;
	}

	public void setPwdField(JPasswordField pwdField) {
		this.pwdField = pwdField;
	}

	public void setPwdLabel(JLabel pwdLabel) {
		this.pwdLabel = pwdLabel;
	}

	public void setRemoveButton(JButton removeButton) {
		this.removeButton = removeButton;
	}

	public void setServerField(JTextField serverField) {
		this.serverField = serverField;
	}

	public void setServerLabel(JLabel serverLabel) {
		this.serverLabel = serverLabel;
	}

	public void setTaskFrame(TaskFrame taskFrame) {
		this.taskFrame = taskFrame;
	}
}
